package com.sky.controller.user;

import com.sky.dto.AddressBookAddDTO;
import com.sky.dto.AddressBookSetDefaultDTO;
import com.sky.dto.AddressBookUpdateDTO;
import com.sky.entity.AddressBook;
import com.sky.result.Result;
import com.sky.service.AddressBookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequestMapping("/user/addressBook")
@RestController
public class AddressBookController {
    @Autowired
    private AddressBookService addressBookService;
    /**
     * 查询收货地址
     * */
    @GetMapping("/list")
    public Result<List<AddressBook>>list(){
       List<AddressBook>list= addressBookService.list();
       return Result.success(list);
    }
    /**
     * 新增收获地址
     * */
    @PostMapping
    public Result add(@RequestBody AddressBookAddDTO addressBookAddDTO){
        addressBookService.add(addressBookAddDTO);
        return Result.success( );
    }
    /**
     * 编辑地址回显
     * */
    @GetMapping("/{id}")
    public Result<AddressBook>getById(@PathVariable("id")Long id){
       AddressBook addressBook= addressBookService.getById(id);
       return Result.success(addressBook);
    }
    /**
     * 根据id修改售货地址
     * */
    @PutMapping
    public  Result update(@RequestBody AddressBookUpdateDTO addressBookUpdateDTO){
        addressBookService.update(addressBookUpdateDTO);
        return Result.success();
    }
    /**
     * 修改默认地址
     * */
    @PutMapping("/default")
    public Result setDefault(@RequestBody AddressBookSetDefaultDTO dto){
        addressBookService.setDefault(dto);
        return Result.success();
    }
    /**
     * 删除地址
     * */
    @DeleteMapping()
    public Result deleteById(Long id){
        addressBookService.deleteById(id);
        return Result.success();
    }


}
